var dns = require('native-dns'),
  server = dns.createServer();

var dnsConfig = {
  port : 53,
  ip : '127.0.0.1'
};
var dnsIps = {};

function start(config){
  config = config || dnsConfig;
  console.dir(config);
  server.on('request', function (request, response) {
    var name = request.question[0].name;
    console.log('name:' + name);
    var ipAddress = getIP(name);
    if(ipAddress){
      response.answer.push(dns.A({
        name: name,
        address: ipAddress,
        ttl: 10
      }));
      response.send();
    }
  });

  server.on('error', function (err, buff, req, res) {
    console.log(err.stack);
  });

  server.serve(dnsConfig.port, dnsConfig.ip);
}

function setIP(name, ip){
  if(arguments.length === 2){
    dnsIps[name] = ip;
  }
}

function getIP(name){
  return dnsIps[name] || null;
}

module.exports = {
  start : start,
  setIP : setIP
};

// start();